<template>
	<view class="container">
		<!-- 轮播 -->
		<view class="swiper-box">
			<u-swiper :list="swiperList" height="300" interval="8000" mode="rect" @click="swiperClick"></u-swiper>
		</view>
		<!-- 菜单 -->
		<view class="menu-box">
			<u-grid :col="4" :border="false" align="center">
				<u-grid-item v-for="(item, index) in menuList" :key="index">
					<u-icon :name="item.name" :size="46"></u-icon>
					<view class="menu-text">{{item.title}}</view>
				</u-grid-item>
			</u-grid>
		</view>
		<u-line color="#f4f4f5" />
		<!-- 公告 -->
		<view class="section-box">
			<u-section title="公告" sub-title="查看更多" lineColor="#2979ff" font-size="32"></u-section>
			<u-cell-group :border="false">
				<u-cell-item v-for="(item, index) in noticeList" :key="index" 
					:title="item.title" :value="item.date" :arrow="false" :title-style="noticeTitleStyle"
					></u-cell-item>
			</u-cell-group>
		</view>
		<!-- 图集 -->
		<view class="section-box">
			<u-section title="图集" sub-title="查看更多" lineColor="#2979ff" font-size="32"></u-section>
			<view class="photo-box">
				<u-swiper :list="photoList" :current="photoCurrent" height="300" interval="5000" effect3d effect3d-previous-margin="200"
				 mode="none" title bg-color="#FFFFFF" :circular="false" @click="photoClick" @change="photoChange"></u-swiper>
				 <view class="arrow-left" @click="changePhoto(1)"><u-icon name="arrow-left" color="#fff" size="40"></u-icon></view>
				 <view class="arrow-right" @click="changePhoto(2)"><u-icon name="arrow-right" color="#fff" size="40"></u-icon></view>
			</view>
		</view>
		<!-- 文章 -->
		<view class="section-box">
			<u-section title="文章" sub-title="查看更多" lineColor="#2979ff" font-size="32"></u-section>
			<u-cell-group :border="false">
				<u-cell-item v-for="(item, index) in articleList" :key="index" 
					:title="item.title" :value="item.date" :arrow="false" :title-style="noticeTitleStyle"
					></u-cell-item>
			</u-cell-group>
		</view>
	</view>
</template>

<script>
	import { api_getSwiper, api_getNotice, api_getArticle } from '@/api/index.js';
	
	export default {
		data() {
			return {
				swiperList: [],
				noticeList: [],
				articleList: [],
				photoList: [],
				photoCurrent: 0,
				menuList: [
					{ title: "图集", name: "photo" },
					{ title: "文章", name: "file-text" },
					{ title: "赛事", name: "clock" },
					{ title: "数据", name: "list" },
				],
				noticeTitleStyle: {
					fontSize: "30rpx",
					transform: "translateX(-28rpx)"
				},
			}
		},
		onLoad() {
			this.getData();
		},
		methods: {
			getData() {
				api_getSwiper().then(res => {
					this.swiperList = res.data.data;
					this.photoList = res.data.data;
				})
				api_getNotice().then(res => {
					this.noticeList = res.data.data;
				})
				api_getArticle().then(res => {
					this.articleList = res.data.data;
				})
			},
			swiperClick(index) {
				this.$u.toast(`点击了第${index + 1}张轮播图`);
			},
			photoClick(index) {
				this.$u.toast(`点击了第${index + 1}张图集`);
			},
			photoChange(index) {
				this.photoCurrent = index;
			},
			changePhoto(index) {
				// index: 1 上一张 2 下一张
				let current = this.photoCurrent;
				let change = null;
				if(index === 1) {
					if(current === 0) return;
					change = current - 1;
				} else {
					if(current === this.photoList.length -1) return;
					change = current + 1;
				}
				this.photoCurrent = change;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.swiper-box {
		width: 100%;
		height: 300rpx;
	}
	
	.menu-box {
		width: 100%;
		height: 150rpx;
	}
	
	.menu-text {
		margin-top: 10rpx;
		font-size: 28rpx;
	}
	
	.section-box {
		padding: 20rpx;
		background-color: #FFFFFF;
	}
	
	.photo-box {
		width: 100%;
		padding-top: 30rpx;
		position: relative;
	}
	
	.photo-box .arrow-left,
	.photo-box .arrow-right {
		position: absolute;
		top: 50%;
		width: 50rpx;
		height: 80rpx;
		transform: translateY(-30rpx);
		background-color: rgba(0, 0, 0, .5);
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.arrow-left {
		left: 0;
	}
	
	.arrow-right {
		right: 0;
	}
</style>
